package com.gears_software.bbnetwork.activitys;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.CommonUtils;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.net.NetworkUtil;

/**
 * 设置密码界面
 * @author DY-PC
 *
 */
@EActivity(R.layout.activity_setpwd)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class SetPwdActivity extends Activity {
	
	@ViewById
	RelativeLayout layout_old;//旧密码的布局
	
	@ViewById
	EditText edit_new_pwd;//新密码
	
	@ViewById
	EditText eidt_old_pwd;//旧密码
	
	@ViewById
	EditText edit_repeat_pwd;//重复密码
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	@Bean
	ToastUI toast;
	
	String hasCashPassword;
	
	@AfterViews
	void onCreate(){
		hasCashPassword = sp.hasCashPassword().get();//是否有提现密码
		
		if(hasCashPassword!=null&&hasCashPassword.equals("0")){
			layout_old.setVisibility(View.GONE);
		}
		
		edit_new_pwd.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if(!arg1){
					String newpwd = edit_new_pwd.getText().toString();
					boolean isY = CommonUtils.isPwd(newpwd);
					if(!isY){
						Toast.makeText(SetPwdActivity.this, "密码格式不正确，请输入字母加数字，6-20位密码！", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
	}
	
	//提交
	@Click
	void btn_confirm(){
		boolean isnet = NetworkUtil.checkNetworkInfo(this);
		if(!isnet){
			showTxt("请检查网络是否正常！");
			return;
		}
		String oldpwd = eidt_old_pwd.getText().toString();
		String newpwd = edit_new_pwd.getText().toString();
		String repeatpwd = edit_repeat_pwd.getText().toString();
		boolean isY = CommonUtils.isPwd(newpwd);
		if(!isY){
			showTxt("密码格式不正确，请重新输入");
			return;
		}
		if(!newpwd.equals(repeatpwd)){
			showTxt("两次密码不一致");
			return;
		}
		if(hasCashPassword.equals("1")){
			//有密码的状态下修改
			if(oldpwd==null||oldpwd.equals("")){
				showTxt("旧密码不可为空！");
				return;
			}
			if(oldpwd.equals(newpwd)){
				showTxt("旧密码和新密码不能一致！");
				return;
			}
			updatePwd(oldpwd, newpwd);
		}else{
			//无密码状态下
			setPwd(newpwd);
		}
	}
	
	@Background
	void updatePwd(String oldCashPassword,String newCashPassword){
		ResultModel<String> res = finder.getUpdateCashPws(sp.userId().get(), oldCashPassword, newCashPassword);
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			showTxt("密码设置成功！");
			finish();
		}else{
			showTxt(res.getMessage());
		}
	}
	
	@Background
	void setPwd(String cashPassword){
		ResultModel<String> res = finder.getSetCashPws(sp.userId().get(), cashPassword);
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			showTxt("密码设置成功！");
			sp.hasCashPassword().put("1");
			finish();
		}else{
			showTxt(res.getMessage());
		}
	}
	
	
	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}
	
	//后退
	@Click
	void image_back(){
		finish();
	}
	
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 AVAnalytics.onResume(this);
		 
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}
}
